//
//  SettingBackgroundViewController.m
//  clublbiza
//
//  Created by WANG TOM on 8/24/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SettingBackgroundViewController.h"


@interface SettingBackgroundViewController ()

@end

@implementation SettingBackgroundViewController
@synthesize popover;
@synthesize uiImageViewPreview;
@synthesize segmentType;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self refreshPreviewImag];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.uiImageViewPreview = nil;
    self.segmentType = nil;
     [self.popover release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}
- (IBAction)segmentSelected:(id)sender
{
    [self refreshPreviewImag];
}
- (NSString*) getUserDefaultKeyName
{
     NSString *defaultKeyName = nil;
    switch (self.segmentType.selectedSegmentIndex) {
        case 0:
            defaultKeyName = APPLICATION_BACKGROUND_IMAGE_NAME_1;

            break;
        case 1:
            defaultKeyName = APPLICATION_BACKGROUND_IMAGE_NAME_2;

            break;
        case 2:
            defaultKeyName = APPLICATION_BACKGROUND_IMAGE_NAME_3;
            break;
        default:
            break;
    }
    return defaultKeyName;
}
- (NSString*) getDefaultBackgrounImageName
{
    NSString *defaultImageFileName = nil;
    switch (self.segmentType.selectedSegmentIndex) {
        case 0:
            defaultImageFileName = @"bg1.jpg";
            
            break;
        case 1:
            defaultImageFileName = @"bg2.jpg";
            
            break;
        case 2:
            defaultImageFileName = @"bg3.jpg";
            break;
        default:
            break;
    }
    return defaultImageFileName;
}
- (IBAction)cleanCustomizeBackgroundImage:(id)sender
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:[self getUserDefaultKeyName]];
    [self refreshPreviewImag];
    
}
- (void) refreshPreviewImag
{
    
    NSString *defaultKeyName = [self getUserDefaultKeyName];
    NSString *defaultImageFileName = [self getDefaultBackgrounImageName];
      
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *isCustomize = [userDefaults stringForKey:defaultKeyName];
    if (isCustomize != nil){
        NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        
        
        NSString *bgImgPath = [documentsPath stringByAppendingFormat:@"/%@",defaultKeyName];
        UIImage *image = [UIImage imageWithContentsOfFile:bgImgPath];
        self.uiImageViewPreview.image = image;
    }else{
        UIImage *image = [UIImage imageNamed:defaultImageFileName];
        self.uiImageViewPreview.image = image;
    }
}
- (IBAction)openImageSelector:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
      
        
        UIImagePickerController* vc = [[UIImagePickerController alloc] init]; 
        vc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
         vc.delegate = self; 
         //vc.contentSizeForViewInPopover = CGSizeMake(800, 400);
        
        
        
       
        
        UIPopoverController *popoverVC = [[UIPopoverController alloc] initWithContentViewController:vc];
        //[popoverVC presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
 
        [popoverVC presentPopoverFromRect:CGRectMake(280,180, 300, 300) inView:self.view permittedArrowDirections:NO animated:NO];

        self.popover = popoverVC;
       // popoverVC.popoverContentSize = CGSizeMake(800.0, 400.0);
        

        [vc release];
        [popoverVC release];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"打开相册错误"
                              message:@""
                              delegate:nil
                              cancelButtonTitle:@"OK!"
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

#pragma mark UIImagePickerControllerDelegate

// this get called when an image has been chosen from the library or taken from the camera
//
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    [self.popover dismissPopoverAnimated:NO];
    //self.uiImageViewPreview.image = image;
    
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
  
    NSString *bgImgPath = [documentsPath stringByAppendingFormat:@"/%@",[self getUserDefaultKeyName]];
    
    [UIImageJPEGRepresentation(image, 1.0) writeToFile:bgImgPath atomically:YES];
     
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setValue:@"1" forKey:[self getUserDefaultKeyName]];
    
    [self refreshPreviewImag];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    // tell our delegate we are finished with the picker
}

@end
